package at.ac.tuwien.infosys.aic11.services;


import javax.jws.WebService;

import at.ac.tuwien.infosys.aic11.dto.CreditRequest;
import at.ac.tuwien.infosys.aic11.dto.Offer;
import at.ac.tuwien.infosys.aic11.fault.UnknownCustomerFault;
import at.ac.tuwien.infosys.aic11.fault.UnknownDurationFault;
import at.ac.tuwien.infosys.aic11.fault.UnknownMoneyFault;
import at.ac.tuwien.infosys.aic11.fault.UnknownRatingFault;
import at.ac.tuwien.infosys.aic11.fault.UnknownRequestFault;
import at.ac.tuwien.infosys.aic11.legacy.ContractManagementMock;


import java.util.logging.Logger;

@WebService(serviceName = "ContractManagementService" ,endpointInterface = "at.ac.tuwien.infosys.aic11.services.ContractManagement",targetNamespace = "http://services.aic11.infosys.tuwien.ac.at/")
public class ContractManagementImpl implements ContractManagement {

	private static Logger log = Logger.getLogger("ContractManagementImpl");
	static {
		log.setParent(Logger.getLogger("at.ac.tuwien.infosys.aic11.services"));
	}
	
	private ContractManagementMock crm = ContractManagementMock.getInstance();
	
	@Override
	public Offer getOffer(CreditRequest cr) throws UnknownRequestFault, UnknownCustomerFault, UnknownMoneyFault, UnknownRatingFault, UnknownDurationFault {
		//rating
		log.info("Received call to getOffer()");
		
		if(cr==null) {
            log.severe("getOffer: argument of type CreditRequest may not be null");
            UnknownRequestFault flt = new UnknownRequestFault("getOffert cannot be called with a null argument");
            log.severe(flt.toString());
            throw flt;
        }
		log.info(cr.toString());
		
		if(cr.getCustomer()==null) {
	       	log.severe("getOffer: argument of type CreditRequest has to contain a Customer object.");
	       	UnknownCustomerFault flt = new UnknownCustomerFault("getOffer: argument of type CreditRequest has to contain a Customer object.");
	        log.severe(flt.toString());
            throw flt;
	    }
		if(cr.getCustomer().getRating()==null) {
	       	log.severe("getOffer: argument of type CreditRequest has to contain a Customer that contains a Rating object.");
	       	UnknownRatingFault flt = new UnknownRatingFault("getOffer: argument of type CreditRequest has to contain a Customer that contains a Rating object.");
	        log.severe(flt.toString());
            throw flt;
	    }
		if(cr.getMoney()==null) {
	       	log.severe("getOffer: argument of type CreditRequest has to contain a Money object.");
	       	UnknownMoneyFault flt = new UnknownMoneyFault("getOffer: argument of type CreditRequest has to contain a Money object.");
	        log.severe(flt.toString());
            throw flt;
	    }
		if(cr.getDuration()==null) {
	       	log.severe("getOffer: argument of type CreditRequest has to contain a Duration object.");
	       	UnknownDurationFault flt = new UnknownDurationFault("getOffer: argument of type CreditRequest has to contain a Duration object.");
	        log.severe(flt.toString());
            throw flt;
	    }
		
		Offer offer = crm.generateOffer(cr);
		
		log.info("Returning offer: " + offer.toString());
		return offer;
	}

	@Override
	public void deleteOffer(CreditRequest cr) throws UnknownRequestFault {
		log.info("got call deleting offer for credit request with id "+cr.getRequestId());
		
		if(cr==null) {
		    log.severe("getOffer: argument of type CreditRequest may not be null");
		    UnknownRequestFault flt = new UnknownRequestFault("deleteOffert cannot be called with a null argument");
		    log.severe(flt.toString());
		    throw flt;
		}

		crm.deleteRequest(cr);
		log.info("Offer deleted");
	}
}
